import api from '@/assets/api/auth'
import apiMy from '@/assets/api/my'
import utils from '@/assets/utils'
const user = {
  state: {
    token: '',
    mobile: '',
    type: '',
    orgId: '',
    isCheck: false
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_MOBILE: (state, mobile) => {
      state.mobile = mobile
    },
    SET_TYPE: (state, type) => {
      state.type = type
    },
    SET_ORGID: (state, orgId) => {
      state.orgId = orgId
    },
    SET_CHECK: (state, isCheck) => {
      state.isCheck = isCheck
    }
  },
  actions: {
    GetInfo ({ commit, dispatch, state }) { // 获取用户信息
      return new Promise((resolve, reject) => {
        api.getInfo().then(response => {
          const data = response.data.data
          commit('SET_MOBILE', data.mobile)
          commit('SET_TYPE', data.type)
          commit('SET_ORGID', data.orgId)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    IsCheck ({ commit, state }) { // 是否待审核
      apiMy.isCheck().then(res => {
        let data = res.data.data
        commit('SET_CHECK', data)
      })
    },
    // 登出
    LogOut ({ commit, state }) {
      commit('SET_MOBILE', '')
      commit('SET_TYPE', '')
      commit('SET_ORGID', '')
      commit('SET_CHECK', false)
      utils.removeCookies('token')
    },
    // 前端 登出
    FedLogOut ({ commit }) {
      return new Promise(resolve => {
        commit('SET_MOBILE', '')
        commit('SET_TYPE', '')
        commit('SET_ORGID', '')
        commit('SET_CHECK', false)
        utils.removeCookies('token')
        resolve()
      })
    }
  }
}

export default user
